*------------------------------------------------------------------------------*
					***MERGE VOTING, POLLUTION AND WEATHER***
*------------------------------------------------------------------------------*

***Use pollution and weather monitor locations. Election dates in each Lander.

**Pollution
use station longitude latitude using "${rawdata}pollution/meta_station_UBA.dta", clear

rename station 		station_UBA
rename longitude_d 	longitude_UBA
rename latitude_d  	latitude_UBA

gen n_UBA = _n

count
local N_UBA = r(N)

qui compress
save "${tempdata}stations_UBA.dta", replace


**Weather
use station_DWD longitude latitude using "${rawdata}pollution/meta_station_DWD.dta", clear

rename longitude_d longitude_DWD
rename latitude_d  latitude_DWD

gen n_DWD = _n

count
local N_DWD = r(N)

qui compress
save "${tempdata}stations_DWD.dta", replace


**Lander election dates
local lander "Baden_Württemberg Bayern Berlin Brandenburg Bremen Hamburg Hessen Mecklenburg_Vorpommern Niedersachsen Nordrhein_Westfalen Rheinland_Pfalz Saarland Sachsen Sachsen_Anhalt Schleswig_Holstein Thüringen"

foreach l of local lander {

	use kreis_code Kreis election_date Land using "${tempdata}/`l'_elections.dta", clear

	bys election_date: keep if _n==1

	gen year 	= year(election_date)
	gen month 	= month(election_date)
	gen day 	= day(election_date)

	gen n_el_`l' = _n

	count
	local N_el_`l' = r(N)

	qui compress
	save "${tempdata}election_dates_`l'.dta", replace

} // l



*------------------------------------------------------------------------------*

***Distance between kreise/municipality centroids and pollution/weather stations

*Selection of pollutants/weather variables
//Pollution
forval i = 1/30 {
	local PM10_lags "`PM10_lags' lag_PM10_ug_`i'"
	local PM10_leads "`PM10_leads' lead_PM10_ug_`i'"
	local O3_lags "`O3_lags' lag_O3_ug_`i'"
	local O3_leads "`O3_leads' lead_O3_ug_`i'"
} // i
forval i = 1/5 {
	local PM10_sunday_lags "`PM10_sunday_lags' lag_PM10_ug_s`i'"
	local PM10_sunday_leads "`PM10_sunday_leads' lead_PM10_ug_s`i'"
} // i
local poll_lagsleags " `PM10_lags' `PM10_leads' `O3_lags' `O3_leads' `PM10_sunday_lags' `PM10_sunday_leads' "
local vars_UBA " PM10_ug PM2_5_mg O3_ug NO2_ug `poll_lagsleags' "

//Weather
forval i = 1/30 {
	local temp_lags "`temp_lags' lag_temp_`i'"
	local temp_leads "`temp_leads' lead_temp_`i'"
	local humidity_lags "`humidity_lags' lag_humid_rel_`i'"
	local humidity_leads "`humidity_leads' lead_humid_rel_`i'"
	local wind_lags "`wind_lags' lag_wind_speed_`i'"
	local wind_leads "`wind_leads' lead_wind_speed_`i'"
	local precip_lags "`precip_lags' lag_precip_`i'"
	local precip_leads "`precip_leads' lead_precip_`i'"
} // i
local weather_lagsleads " `temp_lags' `temp_leads' `humidity_lags' `humidity_leads' `wind_lags' `wind_leads' `precip_lags' `precip_leads' "
local vars_DWD " temp humid_rel wind_speed precip `weather_lagsleads' " 

//Radiuses
local radius " 20 30 40 50 "


**Loop over pollution and weather data
foreach l of local lander {

foreach r of local radius {
	
	foreach st in UBA DWD {

		use "${tempdata}/`l'_centroids.dta", clear
	
		*Create all possible combinations of municipalities and pollution stations
		expand `N_`st''
	
		bys kreis_code: gen n_`st' = _n

		merge m:1 n_`st' using "${tempdata}stations_`st'.dta", keep(master match) nogen 

		rename longitude_d longitude_kreis
		rename latitude_d  latitude_kreis

		*Compute distance
		distance latitude_kreis longitude_kreis latitude_`st' longitude_`st' , a(kreis) b(station)
	
		*Restrict to muni-monitor combinations within certain radius
		keep if dist_KRST <= `r'
	

		*Merge election dates 
		expand `N_el_`l''
		bys kreis_code station_`st' : gen n_el_`l' = _n

		merge m:1 n_el_`l' using "${tempdata}election_dates_`l'.dta", keep(match) nogen


		**Merge pollution/weather on election day		

		*Merge
	
		if "`st'"=="UBA" rename station_UBA station
	
		if "`st'"=="UBA" merge m:1 station 		year month day using "${tempdata}pollution_UBA_d_19902019_w_av.dta", keepusing(`vars_`st'') keep(master match) nogen
		if "`st'"=="DWD" merge m:1 station_DWD 	year month day using "${tempdata}weather_d_19802019_w_av.dta"		, keepusing(`vars_`st'') keep(master match) nogen
	
		if "`st'"=="UBA" recode `vars_`st'' (min/0 = 0)


		**Compute inverse-distance weighted mean for pollutants
		
		foreach p of varlist `vars_`st'' {
			
			preserve

			keep if `p'!=.

			*Create weight
			gen double weight = 1/dist_KRST
			bys kreis_code election_date: egen double weight_tot = total(weight)
			replace weight = weight/weight_tot // to make sure weights sum up to 1
			drop weight_tot

			*Weighted mean
			bys kreis_code election_date: egen double `p'_weight = total(`p'*weight)
			bys kreis_code election_date: keep if _n==1

			keep kreis_code Kreis election_date `p'_weight
		
			rename `p'_weight `p'_weight_`r'km

			qui compress
			save "${tempdata}/`l'_`st'_`p'_`r'.dta", replace

			restore
		} // p

	} // st
	
} // r

} // l


*------------------------------------------------------------------------------*

**Creare full pollution and weather dataset for EACH Lander
local vars_UBA_2 " PM2_5_mg O3_ug NO2_ug `poll_lagsleags' "

foreach l of local lander{

	foreach r of local radius {

		use "${tempdata}/`l'_UBA_PM10_ug_`r'.dta", clear

		*Merge pollution
		foreach p of local vars_UBA_2 {
			merge 1:1 kreis_code election_date using "${tempdata}/`l'_UBA_`p'_`r'.dta", nogen
		} // p

		*Merge weather
		foreach w of local vars_DWD {
			merge 1:1 kreis_code election_date using "${tempdata}/`l'_DWD_`w'_`r'.dta", nogen
		} // w
		
		qui compress
		save "${tempdata}/`l'_p_w_`r'.dta", replace

	} // r
	
	*Merge different radiuses
	use "${tempdata}/`l'_p_w_20.dta", clear
	
	foreach r in 30 40 50 {
		merge 1:1 kreis_code election_date using "${tempdata}/`l'_p_w_`r'.dta", nogen
	} // r
	
	*Save
	qui compress
	save "${tempdata}/`l'_p_w.dta", replace
	
	*Esare files
	foreach r of local radius {
		foreach p of local vars_UBA {
			erase "${tempdata}/`l'_UBA_`p'_`r'.dta"
		} // p

		foreach w of local vars_DWD {
			erase "${tempdata}/`l'_DWD_`w'_`r'.dta"
		} // w
		
		erase "${tempdata}/`l'_p_w_`r'.dta"
	} // r

} // l


*------------------------------------------------------------------------------*

**Create full pollution and weather dataset for ALL Lander
local lander2 " Bayern Berlin Brandenburg Bremen Hamburg Hessen Mecklenburg_Vorpommern Niedersachsen Nordrhein_Westfalen Rheinland_Pfalz Saarland Sachsen Sachsen_Anhalt Schleswig_Holstein Thüringen"

use "${tempdata}/Baden_Württemberg_p_w.dta", clear

*Append
foreach l of local lander2 {
	append using "${tempdata}/`l'_p_w.dta"
} // l

*Save
qui compress
save "${tempdata}/kreise_p_w.dta", replace

*Erase
foreach l of local lander {
	erase "${tempdata}/`l'_p_w.dta"
	erase "${tempdata}/election_dates_`l'.dta"
	erase "${tempdata}/`l'_elections.dta"
} // l



*------------------------------------------------------------------------------*

***Merge datasets
use "${tempdata}kreise_elections.dta", clear

*Votes and pollution
merge m:1 kreis_code election_date using "${tempdata}kreise_p_w.dta", keep(master match) nogen

*Demographics and economics
merge m:1 kreis_code year using "${tempdata}demo_econ_var.dta", keep(master match) nogen
merge m:1 kreis_code year using "${tempdata}Hannover.dta", keep(master match_update) nogen update replace
merge m:1 kreis_code year using "${tempdata}Saarbrucken.dta", keep(master match_update) nogen update replace

qui compress
save "${tempdata}kreise_complete_all.dta", replace

erase "${tempdata}kreise_p_w.dta"
erase "${tempdata}kreise_elections.dta"
erase "${tempdata}Hannover.dta"
erase "${tempdata}Saarbrucken.dta"

*------------------------------------------------------------------------------*

clear

exit
